﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using PCBase.UIControls;

namespace COER
{
    public partial class FormNGTrayData : Form
    {
        public FormNGTrayData()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private int modelNo;
        private NGTrayInfo trayData;
        private string wfID;
        public void ShowForm(int idx, NGTrayInfo info)
        {
            trayData = info;
            propertyGrid1.SelectedObject = trayData;
            modelNo = idx;
            switch (modelNo)
            {
                case 1:
                    {
                        this.Text = "载出NG平台";
                    }
                    break;

            }

            this.Show();
            this.timer1.Start();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        int timecot = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;
            btnInspPass.Visible = false;

            btnDel.Monitor(false, ProcedureHelper_EQ.ManualMode);

            //btnSave.Visible = modelNo == 2;
            //tbxWaferID.Visible = modelNo == 2;

            if (!tbxWaferID.Focused)
            {
                tbxWaferID.Text = wfID;
            }

            if (timecot > 30)
            {
                propertyGrid1.Refresh();
                timecot = 0;
            }
            timecot++;
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "Del":
                    {
                        trayData = new NGTrayInfo();
                        switch (modelNo)
                        {
                            case 1:
                                {
                                    ParameterHelper.ULDSTNGDAL.CopyToHistoryFile(ParameterHelper.FileName_ULDSTNG, string.Format(@"\ProductData\ULDSTNG\{0}\{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));
                                    ParameterHelper.ULDSTNG = trayData;
                                    ProcedureHelper_Exc.SaveULDSTNG();
                                }
                                break;
                        }
                        this.Visible = false;
                    }
                    break;
                case "Save":
                    {

                    }
                    break;
                case "InspPass":
                    {
                      
                    }
                    break;
            }
        }

        private void FormChipData_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            this.timer1.Stop();
            e.Cancel = true;
        }

        private void tbxWaferID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                wfID = tbxWaferID.Text;
                ActiveControl = null;
            }
        }
    }
}
